﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.HtmlControls;
public partial class Main_Master_R_BOM : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    int Counter = 3;
    List<string> LstList;
    List<string> LstItemcode;
    List<string> LstMPN;
    List<string> LstItemName;
    List<string> LstAddlDesc;
    List<string> Lstunitprice;
    List<string> Lstmanufacture;
    List<string> Lsttotalleadtime;
    List<string> LstMoq;
    List<string> Lstvendorname;
    List<string> Lstpicture;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Mode"] == "View")
        {
            pnlrbomview.Visible = true;
            pnlrbom.Visible = false;
            GETRBOMDETALDATA();
        }
        else
        {
            pnlrbomview.Visible = false;
            pnlrbom.Visible = true;
        }
        if (!IsPostBack)
        {
            LstList = new List<string>();
            ViewState["LstList"] = LstList;

            LstItemcode = new List<string>();
            ViewState["LstItemcode"] = LstItemcode;
            
            LstMPN = new List<string>();
            ViewState["LstMPN"]=LstMPN;

            LstItemName = new List<string>();
            ViewState["LstItemName"]=LstItemName;

            LstAddlDesc = new List<string>();
            ViewState["LstAddlDesc"]=LstAddlDesc;

            Lstunitprice = new List<string>();
            ViewState["Lstunitprice"]=Lstunitprice;

            Lstmanufacture = new List<string>();
            ViewState["Lstmanufacture"]=Lstmanufacture;

            Lsttotalleadtime = new List<string>();
            ViewState["Lsttotalleadtime"]=Lsttotalleadtime;

            LstMoq = new List<string>();
            ViewState["LstMoq"]=LstMoq;

            Lstvendorname = new List<string>();
            ViewState["Lstvendorname"]=Lstvendorname;

            Lstpicture = new List<string>();
            ViewState["Lstpicture"] = Lstpicture;
        }
    }
    protected void GETRBOMDETALDATA()
    {
        DataTable dt = DU.getdata("ACC_SELECT_R_BOM_DETAILS");
        if (dt.Rows.Count > 0)
        {
            grdviewrbom.DataSource = dt;
            grdviewrbom.DataBind();
        }
    }
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        LstList = (List<string>)ViewState["LstList"];
        LstItemcode = (List<string>)ViewState["LstItemcode"];
        LstMPN=(List<string>)ViewState["LstMPN"];
        LstItemName = (List<string>)ViewState["LstItemName"];
        LstAddlDesc = (List<string>)ViewState["LstAddlDesc"];
        Lstunitprice = (List<string>)ViewState["Lstunitprice"];
        Lstmanufacture = (List<string>)ViewState["Lstmanufacture"];
        Lsttotalleadtime = (List<string>)ViewState["Lsttotalleadtime"];
        LstMoq = (List<string>)ViewState["LstMoq"];
        Lstvendorname = (List<string>)ViewState["Lstvendorname"];
        Lstpicture = (List<string>)ViewState["Lstpicture"];

        if (LstList != null)
        {
            int count = Convert.ToInt32(LstList.Count.ToString());

            for (int i = 0; i < count; i++)
            {
                Counter++;
                
                Label lbl = new Label();
                lbl.ID = LstList[i].ToString();
                lbl.Text = Counter.ToString();

                HtmlTableRow htrow = new HtmlTableRow();
                htrow.ID = "htrow" + Counter.ToString();

                HtmlTableCell htcell1 = new HtmlTableCell();
                htcell1.ID = "htcell1" + Counter.ToString();
                htcell1.VAlign = "Top";
                TextBox txtitemcode = new TextBox();
                txtitemcode.ID = "txtitemcode" + Counter.ToString();
                txtitemcode.Width = Unit.Percentage(96);
                htcell1.Controls.Add(txtitemcode);
                //LstItemcode.Add(txtitemcode.ID);

                HtmlTableCell htcell2 = new HtmlTableCell();
                htcell2.ID = "htcell2" + Counter.ToString();
                htcell2.VAlign = "Top";
                TextBox txtmpn = new TextBox();
                txtmpn.ID = "txtmpn" + Counter.ToString();
                txtmpn.Width = Unit.Percentage(96);
                htcell2.Controls.Add(txtmpn);
                //LstMPN.Add(txtmpn.ID);

                HtmlTableCell htcell3 = new HtmlTableCell();
                htcell3.ID = "htcell3" + Counter.ToString();
                htcell3.VAlign = "Top";
                TextBox txtitemname = new TextBox();
                txtitemname.ID = "txtitemname" + Counter.ToString();
                txtitemname.Width = Unit.Percentage(96);
                htcell3.Controls.Add(txtitemname);
                //LstItemName.Add(txtitemname.ID);

                HtmlTableCell htcell4 = new HtmlTableCell();
                htcell4.ID = "htcell4" + Counter.ToString();
                htcell4.VAlign = "Top";
                TextBox txtaddldesc = new TextBox();
                txtaddldesc.ID = "txtaddldesc" + Counter.ToString();
                txtaddldesc.Width = Unit.Percentage(96);
                htcell4.Controls.Add(txtaddldesc);
                //LstAddlDesc.Add(txtaddldesc.ID);

                HtmlTableCell htcell5 = new HtmlTableCell();
                htcell5.ID = "htcell5" + Counter.ToString();
                htcell5.VAlign = "Top";
                TextBox txtunitprice = new TextBox();
                txtunitprice.ID = "txtunitprice" + Counter.ToString();
                txtunitprice.Width = Unit.Percentage(96);
                htcell5.Controls.Add(txtunitprice);
                //Lstunitprice.Add(txtunitprice.ID);

                HtmlTableCell htcell6 = new HtmlTableCell();
                htcell6.ID = "htcell6" + Counter.ToString();
                htcell6.VAlign = "Top";
                TextBox txtmanufacture = new TextBox();
                txtmanufacture.ID = "txtmanufacture" + Counter.ToString();
                txtmanufacture.Width = Unit.Percentage(96);
                htcell6.Controls.Add(txtmanufacture);
                //Lstmanufacture.Add(txtmanufacture.ID);

                HtmlTableCell htcell7 = new HtmlTableCell();
                htcell7.ID = "htcell7" + Counter.ToString();
                htcell7.VAlign = "Top";
                TextBox txttotalleadtime = new TextBox();
                txttotalleadtime.ID = "txttotalleadtime" + Counter.ToString();
                txttotalleadtime.Width = Unit.Percentage(96);
                htcell7.Controls.Add(txttotalleadtime);
                //Lsttotalleadtime.Add(txttotalleadtime.ID);

                HtmlTableCell htcell8 = new HtmlTableCell();
                htcell8.ID = "htcell8" + Counter.ToString();
                htcell8.VAlign = "Top";
                TextBox txtmoq = new TextBox();
                txtmoq.ID = "txtmoq" + Counter.ToString();
                txtmoq.Width = Unit.Percentage(96);
                htcell8.Controls.Add(txtmoq);
                //LstMoq.Add(txtmoq.ID);

                HtmlTableCell htcell9 = new HtmlTableCell();
                htcell9.ID = "htcell9" + Counter.ToString();
                htcell9.VAlign = "Top";
                TextBox txtvendorname = new TextBox();
                txtvendorname.ID = "txtvendorname" + Counter.ToString();
                txtvendorname.Width = Unit.Percentage(96);
                htcell9.Controls.Add(txtvendorname);
                //Lstvendorname.Add(txtvendorname.ID);

                HtmlTableCell htcell10 = new HtmlTableCell();
                htcell10.ID = "htcell10" + Counter.ToString();
                htcell10.VAlign = "Top";
                FileUpload FUPicture = new FileUpload();
                FUPicture.ID = "FUPicture" + Counter.ToString();
                htcell10.Controls.Add(FUPicture);
                //Lstpicture.Add(FUPicture.ID);

                htrow.Cells.Add(htcell1);
                htrow.Cells.Add(htcell2);
                htrow.Cells.Add(htcell3);
                htrow.Cells.Add(htcell4);
                htrow.Cells.Add(htcell5);
                htrow.Cells.Add(htcell6);
                htrow.Cells.Add(htcell7);
                htrow.Cells.Add(htcell8);
                htrow.Cells.Add(htcell9);
                htrow.Cells.Add(htcell10);
                tblrbom.Rows.Add(htrow);
            }
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Counter++;
        lblrowno.Text = Convert.ToString(Counter);
        Label lbl = new Label();
        lbl.ID ="lbl"+Counter.ToString();
        lbl.Text = Counter.ToString();
        LstList.Add(lbl.Text);

        HtmlTableRow htrow = new HtmlTableRow();        
        htrow.ID = "htrow" + Counter.ToString();

        HtmlTableCell htcell1 = new HtmlTableCell();
        htcell1.ID = "htcell1" + Counter.ToString();
        htcell1.VAlign = "Top";
        TextBox txtitemcode = new TextBox();
        txtitemcode.ID = "txtitemcode" + Counter.ToString();
        txtitemcode.Width = Unit.Percentage(96);
        htcell1.Controls.Add(txtitemcode);
        LstItemcode.Add(txtitemcode.ID);

        HtmlTableCell htcell2 = new HtmlTableCell();
        htcell2.ID = "htcell2" + Counter.ToString();
        htcell2.VAlign = "Top";
        TextBox txtmpn = new TextBox();
        txtmpn.ID = "txtmpn" + Counter.ToString();
        txtmpn.Width = Unit.Percentage(96);
        htcell2.Controls.Add(txtmpn);
        LstMPN.Add(txtmpn.ID);

        HtmlTableCell htcell3 = new HtmlTableCell();
        htcell3.ID = "htcell3" + Counter.ToString();
        htcell3.VAlign = "Top";
        TextBox txtitemname = new TextBox();
        txtitemname.ID = "txtitemname" + Counter.ToString();
        txtitemname.Width = Unit.Percentage(96);
        htcell3.Controls.Add(txtitemname);
        LstItemName.Add(txtitemname.ID);

        HtmlTableCell htcell4 = new HtmlTableCell();
        htcell4.ID = "htcell4" + Counter.ToString();
        htcell4.VAlign = "Top";
        TextBox txtaddldesc = new TextBox();
        txtaddldesc.ID = "txtaddldesc" + Counter.ToString();
        txtaddldesc.Width = Unit.Percentage(96);
        htcell4.Controls.Add(txtaddldesc);
        LstAddlDesc.Add(txtaddldesc.ID);

        HtmlTableCell htcell5 = new HtmlTableCell();
        htcell5.ID = "htcell5" + Counter.ToString();
        htcell5.VAlign = "Top";
        TextBox txtunitprice = new TextBox();
        txtunitprice.ID = "txtunitprice" + Counter.ToString();
        txtunitprice.Width = Unit.Percentage(96);
        htcell5.Controls.Add(txtunitprice);
        Lstunitprice.Add(txtunitprice.ID);

        HtmlTableCell htcell6 = new HtmlTableCell();
        htcell6.ID = "htcell6" + Counter.ToString();
        htcell6.VAlign = "Top";
        TextBox txtmanufacture = new TextBox();
        txtmanufacture.ID = "txtmanufacture" + Counter.ToString();
        txtmanufacture.Width = Unit.Percentage(96);
        htcell6.Controls.Add(txtmanufacture);
        Lstmanufacture.Add(txtmanufacture.ID);

        HtmlTableCell htcell7 = new HtmlTableCell();
        htcell7.ID = "htcell7" + Counter.ToString();
        htcell7.VAlign = "Top";
        TextBox txttotalleadtime = new TextBox();
        txttotalleadtime.ID = "txttotalleadtime" + Counter.ToString();
        txttotalleadtime.Width = Unit.Percentage(96);
        htcell7.Controls.Add(txttotalleadtime);
        Lsttotalleadtime.Add(txttotalleadtime.ID);

        HtmlTableCell htcell8 = new HtmlTableCell();
        htcell8.ID = "htcell8" + Counter.ToString();
        htcell8.VAlign = "Top";
        TextBox txtmoq = new TextBox();
        txtmoq.ID = "txtmoq" + Counter.ToString();
        txtmoq.Width = Unit.Percentage(96);
        htcell8.Controls.Add(txtmoq);
        LstMoq.Add(txtmoq.ID);

        HtmlTableCell htcell9 = new HtmlTableCell();
        htcell9.ID = "htcell9" + Counter.ToString();
        htcell9.VAlign = "Top";
        TextBox txtvendorname = new TextBox();
        txtvendorname.ID = "txtvendorname" + Counter.ToString();
        txtvendorname.Width = Unit.Percentage(96);
        htcell9.Controls.Add(txtvendorname);
        Lstvendorname.Add(txtvendorname.ID);

        HtmlTableCell htcell10 = new HtmlTableCell();
        htcell10.ID = "htcell10" + Counter.ToString();
        htcell10.VAlign = "Top";
        FileUpload FUPicture = new FileUpload();
        FUPicture.ID = "FUPicture" + Counter.ToString();
        htcell10.Controls.Add(FUPicture);
        Lstpicture.Add(FUPicture.ID);
        //Lstpicture.Add(FUPicture.PostedFile.FileName);
        

        htrow.Cells.Add(htcell1);
        htrow.Cells.Add(htcell2);
        htrow.Cells.Add(htcell3);
        htrow.Cells.Add(htcell4);
        htrow.Cells.Add(htcell5);
        htrow.Cells.Add(htcell6);
        htrow.Cells.Add(htcell7);
        htrow.Cells.Add(htcell8);
        htrow.Cells.Add(htcell9);
        htrow.Cells.Add(htcell10);

        tblrbom.Rows.Add(htrow);

        ViewState["LstList"] = LstList;
        ViewState["LstItemcode"] = LstItemcode;
        ViewState["LstMPN"] = LstMPN;
        ViewState["LstItemName"] = LstItemName;
        ViewState["LstAddlDesc"] = LstAddlDesc;
        ViewState["Lstunitprice"] = Lstunitprice;
        ViewState["Lstmanufacture"] = Lstmanufacture;
        ViewState["Lsttotalleadtime"] = Lsttotalleadtime;
        ViewState["LstMoq"] = LstMoq;
        ViewState["Lstvendorname"] = Lstvendorname;
        ViewState["Lstpicture"] = Lstpicture;
        
        hidsecond.Value = Counter.ToString();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] param = {new SqlParameter("@productcode",txtproductcode.Text),
                                   new SqlParameter("@productname",txtproductname.Text),                                  
                                  };
            int insert = DU.executeprocedure("USP_ACC_SaveRBOM", param);
            int count = Convert.ToInt32(hidsecond.Value);
            if (insert > 0)
            {
                for (int i = 1; i <=count; i++)
                {
                    TextBox txtitemcode = (TextBox)tblrbom.Rows[i-1].FindControl("txtitemcode" + i.ToString());
                    TextBox txtmpn = (TextBox)tblrbom.Rows[i-1].FindControl("txtmpn" + i.ToString());
                    TextBox txtitemname = (TextBox)tblrbom.Rows[i-1].FindControl("txtitemname" + i.ToString());
                    TextBox txtaddldesc = (TextBox)tblrbom.Rows[i-1].FindControl("txtaddldesc" + i.ToString());
                    TextBox txtunitprice = (TextBox)tblrbom.Rows[i-1].FindControl("txtunitprice" + i.ToString());
                    TextBox txtmanufacture = (TextBox)tblrbom.Rows[i-1].FindControl("txtmanufacture" + i.ToString());
                    TextBox txttotalleadtime = (TextBox)tblrbom.Rows[i-1].FindControl("txttotalleadtime" + i.ToString());
                    TextBox txtmoq = (TextBox)tblrbom.Rows[i-1].FindControl("txtmoq" + i.ToString());
                    TextBox txtvendorname = (TextBox)tblrbom.Rows[i-1].FindControl("txtvendorname" + i.ToString());
                    FileUpload FUPicture = (FileUpload)tblrbom.Rows[i-1].FindControl("FUPicture" + i.ToString());
                    string fileimage = "";
                    if (FUPicture.HasFile)
                    {
                        string uniquename = Guid.NewGuid().ToString().Replace("-", string.Empty);
                        FileInfo finfo = new FileInfo(FUPicture.PostedFile.FileName);
                        string ext = finfo.Extension;
                        fileimage = "~/Main/Inventory/Item/Image/" + uniquename + ext;
                        FUPicture.SaveAs(Server.MapPath(fileimage));
                    }                    
                    SqlParameter[] paramdetail = {new SqlParameter("@itemcode",txtitemcode.Text.Trim()),
                                                 new SqlParameter("@mpn",txtmpn.Text.Trim()),
                                                 new SqlParameter("@itemname",txtitemname.Text.Trim()),
                                                 new SqlParameter("@addldesc",txtaddldesc.Text.Trim()),
                                                 new SqlParameter("@unitprice",Convert.ToDecimal(txtunitprice.Text.Trim())),
                                                 new SqlParameter("@manufacture",txtmanufacture.Text.Trim()),
                                                 new SqlParameter("@totalleadtime",txttotalleadtime.Text.Trim()),
                                                 new SqlParameter("@moq",txtmoq.Text.Trim()),
                                                 new SqlParameter("@vendorname",txtvendorname.Text.Trim()),
                                                 new SqlParameter("@picturename",fileimage.ToString())};
                    DU.executeprocedure("USP_ACC_SAVERBOM_DETAIL", paramdetail);
                }
            }
            clearcontrol();
        }
        catch (Exception ex)
        {
        }
    }
    protected void grdviewrbom_DataBound(object sender, EventArgs e)
    {        
        for (int rowIndex = grdviewrbom.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow gvrow = grdviewrbom.Rows[rowIndex];
            GridViewRow gvprevrow = grdviewrbom.Rows[rowIndex + 1];
            for (int cellcount = 0; cellcount < 2; cellcount++)
            {
                if (gvrow.Cells[cellcount].Text == gvprevrow.Cells[cellcount].Text)
                {
                    if (gvprevrow.Cells[cellcount].RowSpan < 2)
                    {
                        gvrow.Cells[cellcount].RowSpan = 2;
                    }
                    else
                    {
                        gvrow.Cells[cellcount].RowSpan = gvprevrow.Cells[cellcount].RowSpan + 1;
                    }
                    gvprevrow.Cells[cellcount].Visible = false;
                }
            }
        }        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        clearcontrol();
    }
    protected void clearcontrol()
    {
        txtproductcode.Text = string.Empty;
        txtproductname.Text = string.Empty;
        int count = Convert.ToInt32(hidsecond.Value);
        for (int i = 1; i <= count; i++)
        {
            TextBox txtitemcode = (TextBox)tblrbom.Rows[i - 1].FindControl("txtitemcode" + i.ToString());
            TextBox txtmpn = (TextBox)tblrbom.Rows[i - 1].FindControl("txtmpn" + i.ToString());
            TextBox txtitemname = (TextBox)tblrbom.Rows[i - 1].FindControl("txtitemname" + i.ToString());
            TextBox txtaddldesc = (TextBox)tblrbom.Rows[i - 1].FindControl("txtaddldesc" + i.ToString());
            TextBox txtunitprice = (TextBox)tblrbom.Rows[i - 1].FindControl("txtunitprice" + i.ToString());
            TextBox txtmanufacture = (TextBox)tblrbom.Rows[i - 1].FindControl("txtmanufacture" + i.ToString());
            TextBox txttotalleadtime = (TextBox)tblrbom.Rows[i - 1].FindControl("txttotalleadtime" + i.ToString());
            TextBox txtmoq = (TextBox)tblrbom.Rows[i - 1].FindControl("txtmoq" + i.ToString());
            TextBox txtvendorname = (TextBox)tblrbom.Rows[i - 1].FindControl("txtvendorname" + i.ToString());
            //FileUpload FUPicture = (FileUpload)tblrbom.Rows[i - 1].FindControl("FUPicture" + i.ToString());
            txtitemcode.Text = ""; txtmpn.Text = ""; txtitemname.Text = ""; txtaddldesc.Text = ""; txtunitprice.Text = "";
            txtmanufacture.Text = ""; txttotalleadtime.Text = ""; txtmoq.Text = ""; txtvendorname.Text = "";
        }
        lblmsg.Visible = true;
        lblmsg.Text = "Inserted SuccessFully.";
    }
    protected void grdviewrbom_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {            
            Image imgproduct = (Image)e.Row.FindControl("imgitem");
            //imgproduct.BorderColor = System.Drawing.Color.White;
            if (imgproduct.ImageUrl == "")
            {
                imgproduct.Style["border"] = "0px none white";
            }
        }        
    }
}